/*
 * Copyright rgil2010
 * All rights reserved
 */

package com.totoBD.gui.swing.components.dialog;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author user
 */
public class DlgConUserCreate extends DlgConUserAbs implements ActionListener{
    public DlgConUserCreate(Frame parent, boolean modal) throws ClassNotFoundException {
        super("Create an user",
              "Enter the name of the user to create :",
              "Give",
              parent,
              modal);

        
        this.txfPassword = new JTextField();
        this.lblPassword = new JLabel("Enter the user password");
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(super.lblTextBoxdef)
                    .addComponent(super.txfUserName)
                    .addComponent(this.lblPassword)
                    .addComponent(this.txfPassword)
                    .addComponent(lblUseDataBase)
                    .addComponent(txfDataBaseName, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                    .addComponent(cbxUseAllDataBases)
                    .addComponent(tmkGuiShrBtnGrpCommand, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap()
                )
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTextBoxdef)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txfUserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblPassword)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txfPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblUseDataBase)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txfDataBaseName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(cbxUseAllDataBases, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(tmkGuiShrBtnGrpCommand, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pack();

    }

    @Override
    protected boolean _doJob() throws  Exception {
        String strUserName = super.txfUserName.getText();
        String strUserPassword = this.txfPassword.getText();
        if (blnUseAllDataBases){
            _dbConnection_.executeUpdate(
                "GRANT SELECT,INSERT,UPDATE,DELETE," +
                "CREATE,DROP " +
                "ON *.* TO '" +
                strUserName +
                "'@'localhost' " +
                "IDENTIFIED BY '" +
                strUserPassword +
                "';");
            return true;
        }
        else{
            String strDatabse = this.txfDataBaseName.getText();
            _dbConnection_.executeUpdate(
                "GRANT SELECT,INSERT,UPDATE,DELETE," +
                "CREATE,DROP " +
                "ON " +
                strDatabse +
                ".* TO '" +
                strUserName +
                "'@'localhost' " +
                "IDENTIFIED BY '" +
                strUserPassword +
                "';");
            return true;
        }
     }

    @Override
    public void actionPerformed(ActionEvent e)
    {
       super.actionPerformed(e);
    }

    private JTextField txfPassword;
    private JLabel lblPassword;
    

}
